package org.example._01_game;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 自动输入
 */
public class _02_RobotTest {
    // 成员变量，存储26个字母的键盘ASCII码
    public static int A = KeyEvent.VK_A;
    public static int B = KeyEvent.VK_B;
    public static int C = KeyEvent.VK_C;
    public static int D = KeyEvent.VK_D;
    public static int E = KeyEvent.VK_E;
    public static int F = KeyEvent.VK_F;
    public static int G = KeyEvent.VK_G;
    public static int H = KeyEvent.VK_H;
    public static int I = KeyEvent.VK_I;
    public static int J = KeyEvent.VK_J;
    public static int K = KeyEvent.VK_K;
    public static int L = KeyEvent.VK_L;
    public static int M = KeyEvent.VK_M;
    public static int N = KeyEvent.VK_N;
    public static int O = KeyEvent.VK_O;
    public static int P = KeyEvent.VK_P;
    public static int Q = KeyEvent.VK_Q;
    public static int R = KeyEvent.VK_R;
    public static int S = KeyEvent.VK_S;
    public static int T = KeyEvent.VK_T;
    public static int U = KeyEvent.VK_U;
    public static int V = KeyEvent.VK_V;
    public static int W = KeyEvent.VK_W;
    public static int X = KeyEvent.VK_X;
    public static int Y = KeyEvent.VK_Y;
    public static int Z = KeyEvent.VK_Z;

    public static List<Integer> asciiList = new ArrayList<Integer>();

    Integer [] strings = {1,2};
    //String [] strings = {"hello","word"};
    public static int delayTime = 350;

    static {
        // 获取所有成员变量，存入list中
        /**
         * getFields()：获得某个类的所有的公共（public）的字段，包括父类中的字段。
         * getDeclaredFields()：获得某个类的所有声明的字段，即包括 public、public和proteced，但是不包括父类的申明字段。
         */
//        Field[] fields = RobotTest.class.getFields();
        Field[] fields = _02_RobotTest.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getType() == int.class) {
                try {
                    int ascii = (int) field.get(_02_RobotTest.class);
                    asciiList.add(ascii);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        // 获取所有成员变量，存入list中
        /**
         * getFields()：获得某个类的所有的公共（public）的字段，包括父类中的字段。
         * getDeclaredFields()：获得某个类的所有声明的字段，即包括public、public和proteced，但是不包括父类的申明字段。
         */
//        Field[] fields = RobotTest.class.getDeclaredFields();
//        List<Integer> asciiList = new ArrayList<>();
//        for (Field field : fields) {
//            try {
//                /**
//                 * get(null) 获取这个对象的 field
//                 */
//                int ascii = (int) field.get(null);
//                asciiList.add(ascii);
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            }
//        }
        // 创建txt文件
        String os = System.getProperty("os.name").toLowerCase();
        String filePath = "test.txt";
        if (os.contains("win")) {
            filePath = "C:\\Temp\\" + filePath;
        } else if (os.contains("mac")) {
            filePath = "/Users/eddy/Desktop/" + filePath;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }

//            Runtime.getRuntime().exec("notepad.exe" + file.getAbsoluteFile());
        // 打开文件
        Desktop desktop = Desktop.getDesktop();
        desktop.edit(file);
//            desktop.open(file);

        // 使用Robot类模拟键盘输出
        Robot robot = new Robot();

        robot.setAutoDelay(2000);

        //4行
        for (int i = 0; i < 4; i++) {
            //4列 以逗号分隔
            for (int j = 0; j < 4; j++) {
                //拼音
                int i1 = new Random().nextInt(4) + 2;
                for (int k = 0; k < i1; k++) {
                    // 随机获取一个ASCII码
                    int ascii = asciiList.get(new Random().nextInt(asciiList.size()));
                    robot.keyPress(ascii);
                    robot.keyRelease(ascii);
                    robot.setAutoDelay(delayTime);
                }
                robot.setAutoDelay(delayTime);
                robot.keyPress(KeyEvent.VK_SPACE);
                robot.keyRelease(KeyEvent.VK_SPACE);
                //逗号
                robot.keyPress(KeyEvent.VK_COMMA);
                robot.setAutoDelay(delayTime);
                //停1s
//                robot.setAutoDelay(1000);
            }
            robot.setAutoDelay(delayTime);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        }

    }
}
